package cn.yj.netty.message.chain;

import cn.yj.netty.common.Const;
import cn.yj.netty.support.test.MessageProtocol;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 对协议内容进行鉴权认证
 *
 * @author 永健
 * @since 2022-02-16 14:05
 */
@Component
public class AuthenticateHandler extends AbstractMessageChain<MessageProtocol> {

    private static final Logger log = LoggerFactory.getLogger(AuthenticateHandler.class);


    @Resource(type = DecryptHandler.class)
    @Override
    public AbstractMessageChain setNextChain(AbstractMessageChain nextChain) {
        return super.setNextChain(nextChain);
    }

    @Override
    void doHandler(ChannelHandlerContext channelHandlerContext, MessageProtocol msg) {
        log.info("对协议包鉴权,host={}", channelHandlerContext.channel().remoteAddress().toString());
        if (!Const.TAG.equals(msg.getHeader().getTag())) {
            log.warn("鉴权失败,host={},TAG={}", getHost(), msg.getHeader().getTag());
            channelHandlerContext.channel().close();
            return;
        }
        getNextChain().handler(channelHandlerContext, msg);
    }
}
